package net.wexoo.onlinedivelog.view.panels.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import net.wexoo.onlinedivelog.view.pages.content.home.Home;
import net.wexoo.onlinedivelog.view.util.DiveLogSession;
import net.wexoo.onlinedivelog.view.util.NotificationPanel;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;

public class LoggedInPanel extends Panel {

   private static final long serialVersionUID = 1L;
   @SuppressWarnings("unused")
   private final NotificationPanel mainNotificationPanel;

   public LoggedInPanel(String id, NotificationPanel mainNotificationPanel) {
      super(id);

      this.mainNotificationPanel = mainNotificationPanel;

      Form<LoggedInPanel> form = new Form<LoggedInPanel>("f", new CompoundPropertyModel<LoggedInPanel>(this)) {

         private static final long serialVersionUID = 1L;

         @Override
         protected void onSubmit() {
            ((DiveLogSession) getSession()).setLoggedInPerson(null);
            setResponsePage(Home.class);
            //            info("Logout button pressed!");
            //            this.mainNotificationPanel.refresh(target);
         }
      };
      add(form);

      form.add(new Label("loggedInUser", " " + ((DiveLogSession) getSession()).getLoggedInPerson().getLogin()));

      List<Locale> supportedLocales = new ArrayList<Locale>();
      supportedLocales.add(Locale.ENGLISH);
      supportedLocales.add(Locale.GERMAN);

      IChoiceRenderer<Locale> choiceRenderer = new IChoiceRenderer<Locale>() {

         private static final long serialVersionUID = 1L;

         @Override
         public String getIdValue(Locale locale, int index) {
            return Integer.toString(index);
         }

         @Override
         public Object getDisplayValue(Locale locale) {
            return locale.getDisplayName(locale);
         }
      };

      DropDownChoice<Locale> localeChoice = new DropDownChoice<Locale>("selectedLocale", new PropertyModel<Locale>(
            getSession(), "locale"), supportedLocales, choiceRenderer) {

         private static final long serialVersionUID = 1L;

         @Override
         protected boolean wantOnSelectionChangedNotifications() {
            return true;
         }

         @Override
         protected void onSelectionChanged(Locale newSelection) {
         }
      };

      form.add(localeChoice);
   }
}